package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC125 {


    public boolean isPalindrome(String str) {
        if (null == str || str.length() <= 1) {
            return true;
        }

        int left = 0, right = str.length() - 1;
        while (left < right) {
            char leftChar = str.charAt(left);
            char rightChar = str.charAt(right);
            if (!isValidateChar(leftChar)) {
                ++left;
                continue;
            }


            if (!isValidateChar(rightChar)) {
                --right;
                continue;
            }

            if (!isMatchChar(leftChar, rightChar)) {
                return false;
            }

            ++left;
            --right;
        }

        return true;
    }

    public boolean isValidateChar(char ch) {
        return (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
    }

    public boolean isMatchChar(char cha, char chb) {
        if ((cha >= '0' && cha <= '9') && (chb >= '0' && chb <= '9')) {
            return cha == chb;
        }

        cha = (cha >= 'A' && cha <= 'Z') ? (char) (cha + 'a' - 'A') : cha;
        chb = (chb >= 'A' && chb <= 'Z') ? (char) (chb + 'a' - 'A') : chb;
        return cha == chb;
    }

    public static void main(String[] args) {
        String test = "race a car";
        LC125 an = new LC125();
        System.out.println(an.isPalindrome(test));
//        System.out.println(an.isValidateChar('r'));
    }
}
